home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / Array2.h < prev    next >
C/C++ Source or Header  |  1996-11-07  |  4KB  |  197 lines

  1. // Template array classes
  2. /*
  3.  
  4. Copyright (C) 1996 John W. Eaton
  5.  
  6. This file is part of Octave.
  7.  
  8. Octave is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2, or (at your option) any
  11. later version.
  12.  
  13. Octave is distributed in the hope that it will be useful, but WITHOUT
  14. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  16. for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with Octave; see the file COPYING.  If not, write to the Free
  20. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22. */
  23.  
  24. #if !defined (octave_Array2_h)
  25. #define octave_Array2_h 1
  26.  
  27. #if defined (__GNUG__)
  28. #pragma interface
  29. #endif
  30.  
  31. #include <cassert>
  32. #include <cstdlib>
  33.  
  34. #include "Array.h"
  35. #include "lo-error.h"
  36.  
  37. class idx_vector;
  38.  
  39. // Two dimensional array class.
  40.  
  41. template <class T>
  42. class Array2 : public Array<T>
  43. {
  44. protected:
  45.  
  46.   Array2 (T *d, int n, int m) : Array<T> (d, n*m)
  47.     {
  48.       d1 = n;
  49.       d2 = m;
  50.       set_max_indices (2);
  51.     }
  52.  
  53. public:
  54.  
  55.   // These really need to be protected (and they will be in the
  56.   // future, so don't depend on them being here!), but they can't be
  57.   // until template friends work correctly in g++.
  58.  
  59.   int d1;
  60.   int d2;
  61.  
  62.   Array2 (void) : Array<T> ()
  63.     {
  64.       d1 = 0;
  65.       d2 = 0;
  66.       set_max_indices (2);
  67.     }
  68.  
  69.   Array2 (int n, int m) : Array<T> (n*m)
  70.     {
  71.       d1 = n;
  72.       d2 = m;
  73.       set_max_indices (2);
  74.     }
  75.  
  76.   Array2 (int n, int m, const T& val) : Array<T> (n*m, val)
  77.     {
  78.       d1 = n;
  79.       d2 = m;
  80.       set_max_indices (2);
  81.     }
  82.  
  83.   Array2 (const Array2<T>& a) : Array<T> (a)
  84.     {
  85.       d1 = a.d1;
  86.       d2 = a.d2;
  87.       set_max_indices (2);
  88.     }
  89.  
  90.   Array2 (const Array<T>& a, int n, int m) : Array<T> (a)
  91.     {
  92.       d1 = n;
  93.       d2 = m;
  94.       set_max_indices (2);
  95.     }
  96.  
  97.   ~Array2 (void) { }
  98.  
  99.   Array2<T>& operator = (const Array2<T>& a)
  100.     {
  101.       if (this != &a && rep != a.rep)
  102.     {
  103.       Array<T>::operator = (a);
  104.       d1 = a.d1;
  105.       d2 = a.d2;
  106.     }
  107.  
  108.       return *this;
  109.     }
  110.  
  111.   int dim1 (void) const { return d1; }
  112.   int dim2 (void) const { return d2; }
  113.  
  114.   int rows (void) const { return d1; }
  115.   int cols (void) const { return d2; }
  116.   int columns (void) const { return d2; }
  117.  
  118.   // No checking of any kind, ever.
  119.  
  120.   T& xelem (int i, int j) { return Array<T>::xelem (d1*j+i); }
  121.   T xelem (int i, int j) const { return Array<T>::xelem (d1*j+i); }
  122.  
  123.   // Note that the following element selection methods don't use
  124.   // xelem() because they need to make use of the code in
  125.   // Array<T>::elem() that checks the reference count.
  126.  
  127.   T& checkelem (int i, int j)
  128.     {
  129.       if (i < 0 || j < 0 || i >= d1 || j >= d2)
  130.     {
  131.       (*current_liboctave_error_handler)
  132.         ("T& Array2<T>::checkelem (%d, %d): range error", i, j);
  133.       static T foo;
  134.       return foo;
  135.     }
  136.       else
  137.     return Array<T>::elem (d1*j+i);
  138.     }
  139.  
  140.   T& elem (int i, int j) { return Array<T>::elem (d1*j+i); }
  141.  
  142. #if defined (BOUNDS_CHECKING)
  143.   T& operator () (int i, int j) { return checkelem (i, j); }
  144. #else
  145.   T& operator () (int i, int j) { return elem (i, j); }
  146. #endif
  147.  
  148.   T checkelem (int i, int j) const
  149.     {
  150.       if (i < 0 || j < 0 || i >= d1 || j >= d2)
  151.     {
  152.       (*current_liboctave_error_handler)
  153.         ("T Array2<T>::checkelem (%d, %d): range error", i, j);
  154.       return T ();
  155.     }
  156.       else
  157.     return Array<T>::elem (d1*j+i);
  158.     }
  159.  
  160.   T elem (int i, int j) const { return Array<T>::elem (d1*j+i); }
  161.  
  162. #if defined (BOUNDS_CHECKING)
  163.   T operator () (int i, int j) const { return checkelem (i, j); }
  164. #else
  165.   T operator () (int i, int j) const { return elem (i, j); }
  166. #endif
  167.  
  168.   T range_error (const char *fcn, int i, int j) const;
  169.   T& range_error (const char *fcn, int i, int j);
  170.  
  171.   void resize (int n, int m);
  172.   void resize (int n, int m, const T& val);
  173.  
  174.   Array2<T>& insert (const Array2<T>& a, int r, int c);
  175.  
  176. #ifdef HEAVYWEIGHT_INDEXING
  177.   void maybe_delete_elements (idx_vector& i, idx_vector& j);
  178.  
  179.   Array2<T> value (void);
  180.  
  181.   Array2<T> index (idx_vector& i) const;
  182.  
  183.   Array2<T> index (idx_vector& i, idx_vector& j) const;
  184. #endif
  185. };
  186.  
  187. template <class LT, class RT>
  188. int assign (Array2<LT>& lhs, const Array2<RT>& rhs);
  189.  
  190. #endif
  191.  
  192. /*
  193. ;;; Local Variables: ***
  194. ;;; mode: C++ ***
  195. ;;; End: ***
  196. */
  197.